﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
using System.Linq;
using System.Runtime.Serialization;
using KPIS.GERP.Models.HRM;

namespace KPIS.GERP.Models.SYS
{
    [DataContract, Table("SysTask")]
    public class Task
    {
        public Task()
        {
            TaskHistories = new HashSet<TaskHistory>();
            Simulates = new HashSet<ApprovePathSimulate>();
        }

        [DataMember, Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public virtual int TaskId { get; set; }

        [DataMember]
        public string Code { get; set; }

        [DataMember]
        public string Title { get; set; }

        #region Created By

        [DataMember, DefaultValue("U")]
        public virtual string RecordStatus { get; set; }

        [DataMember, ForeignKey("CreateByEmployee")]
        public virtual int? CreatedBy { get; set; }

        [DataMember]
        public virtual DateTime? CreatedWhen { get; set; }

        [DataMember]
        public virtual int? UpdatedBy { get; set; }

        [DataMember]
        public virtual DateTime? UpdatedWhen { get; set; }

        [DataMember]
        public virtual int? ApprovedBy { get; set; }

        [DataMember]
        public virtual string ApprovedByPosName { get; set; }

        [DataMember]
        public virtual string ApprovedByFullName { get; set; }

        [DataMember]
        public virtual DateTime? ApprovedWhen { get; set; }

        [DataMember]
        public virtual int? RejectedBy { get; set; }

        [DataMember]
        public virtual string RejectedByPosName { get; set; }

        [DataMember]
        public virtual string RejectedByFullName { get; set; }

        [DataMember]
        public virtual DateTime? RejectedWhen { get; set; }

        [DataMember]
        public virtual int? CanceledBy { get; set; }

        [DataMember]
        public virtual string CanceledByPosName { get; set; }

        [DataMember]
        public virtual string CanceledByFullName { get; set; }

        [DataMember]
        public virtual DateTime? CanceledWhen { get; set; }
        
        [DataMember]
        public virtual bool Completed { get; set; }

        [DataMember]
        public virtual string CompletedByPosName { get; set; }

        [DataMember]
        public virtual string CompletedByFullName { get; set; }

        [DataMember]
        public virtual int? CompletedBy { get; set; }

        [DataMember]
        public virtual DateTime? CompletedWhen { get; set; }

        #endregion

        [DataMember, ForeignKey("CurrentTaskHistory")]
        public int? CurrentTaskHistoryId { get; set; }

        [DataMember]
        public TaskHistory CurrentTaskHistory { get; set; }

        [DataMember]
        public ICollection<TaskHistory> TaskHistories { get; set; }
        
        [DataMember]
        public ICollection<ApprovePathSimulate> Simulates { get; set; }
        
        [DataMember, ForeignKey("ApplyToEmployee")]
        public int ApplyToEmpId { get; set; }
        
        [DataMember, ForeignKey("RelatedToEmployee")]
        public int? RelatedTo { get; set; }

        [DataMember, ForeignKey("ApproveProcess")]
        public int AppPrcId { get; set; }
        
        [DataMember]
        public ApproveProcess ApproveProcess { get; set; }

        [DataMember]
        public int RefId { get; set; }

        [DataMember]
        public decimal Amount { get; set; }

        [DataMember]
        public string RefCode { get; set; }

        [DataMember]
        public string CreateResult { get; set; }

        [DataMember]
        public string CreateErrorResult { get; set; }

        [DataMember]
        public string Remark { get; set; }

        [DataMember]
        public string SystemRemark { get; set; }

        [DataMember]
        public string ConfirmAs { get; set; }

        [DataMember]
        public string ApproveAs { get; set; }

        [DataMember]
        public Employee ApplyToEmployee { get; set; }

        [DataMember]
        public Employee RelatedToEmployee { get; set; }

        [DataMember]
        public Employee CreateByEmployee { get; set; }

        [DataMember]
        public string LastestActionCode { get; set; }

        [DataMember]
        public int? LastestActionBy { get; set; }

        [DataMember]
        public string LastestActionByPosName { get; set; }

        [DataMember]
        public string LastestActionByFullName { get; set; }

        [DataMember]
        public bool IsInRequiringSendBack { get; set; }

        public string StatusText
        {
            get
            {
                if (ApprovedBy.HasValue)
                {
                    return "ผ่านการอนุมัติแล้ว โดย " + ApprovedByPosName + "      " + ApprovedByFullName + " เมื่อ " + ApprovedWhen.Value.ToString("วันddddที่ d MMMM yyyy เวลา HH:mm");
                }
                else if (CanceledBy.HasValue)
                {
                    return "ยกเลิกคำขอนี้แล้ว โดย " + CanceledByPosName + "      " + CanceledByFullName + " เมื่อ " + CanceledWhen.Value.ToString("วันddddที่ d MMMM yyyy เวลา HH:mm");
                }
                else if (RejectedBy.HasValue)
                {
                    return "ไม่ผ่านการอนุมัติ โดย " + RejectedByPosName + "      " + RejectedByFullName + " เมื่อ " + RejectedWhen.Value.ToString("วันddddที่ d MMMM yyyy เวลา HH:mm");
                }
                else if (CompletedBy.HasValue)
                {
                    return "สิ้นสุดกระบวนการแล้ว โดย " + CompletedByPosName + "      " + CompletedByFullName + " เมื่อ " + CompletedWhen.Value.ToString("วันddddที่ d MMMM yyyy เวลา HH:mm");
                }
                else if (IsInRequiringSendBack)
                {
                    // use previos task history
                    var db = new GERPContext(true);
                    var previosTaskHistory = db.TaskHistories
                        .Include(o=>o.Task.CreateByEmployee.Position)
                        .Where(o => o.TaskId == this.TaskId)
                        .OrderByDescending(o => o.TaskHistoryId).Skip(1)
                        .FirstOrDefault();

                    string strSendBackBy = "";
                    string strCreatedBy = "";
                    string strSendBackWhen = "";
                    if(previosTaskHistory != null)
                    {
                        strSendBackBy = previosTaskHistory.SendBackByPosName + "   " + previosTaskHistory.SendBackByFullName;
                        strSendBackWhen = !previosTaskHistory.SendBackWhen.HasValue ? "" : previosTaskHistory.SendBackWhen.Value.ToString("วันddddที่ d MMMM yyyy เวลา HH:mm");
                        if(previosTaskHistory.Task != null && 
                            previosTaskHistory.Task.CreateByEmployee != null && 
                            previosTaskHistory.Task.CreateByEmployee.Position != null)
                        {
                            strCreatedBy = previosTaskHistory.Task.CreateByEmployee.Position.PosNo + " - " +
                            previosTaskHistory.Task.CreateByEmployee.Position.Name + "   " +
                            previosTaskHistory.Task.CreateByEmployee.FullName;
                        }
                    }


                    return string.Format("ถูกส่งกลับมาจาก {0} ส่งถึงผู้ทำคำขอ {1} เมื่อ {2}", strSendBackBy, strCreatedBy, strSendBackWhen);
                }
                else if (CurrentTaskHistory != null)
                {
                    if(CurrentTaskHistory.ApproverPosition == null)
                    {
                        var db = new GERPContext(false);
                        CurrentTaskHistory.ApproverPosition = db.Positions.FirstOrDefault(o => o.PosId == CurrentTaskHistory.ApproverPosId);
                    }

                    string posNo;
                    string posName;
                    if(CurrentTaskHistory.ApproverPosition != null)
                    {
                        posNo = Convert.ToString(CurrentTaskHistory.ApproverPosition.PosNo);
                        posName = CurrentTaskHistory.ApproverPosition.Name;
                    } else
                    {
                        posNo = "(โหลดตำแหน่งไม่ได้)";
                        posName = "(โหลดตำแหน่ง_" + CurrentTaskHistory.ApproverPosId + "_ไม่ได้)";
                    }
                    return string.Format("กำลังรอ {0} - {1} พิจารณาอยู่", posNo, posName);
                }
                else
                {
                    return "";
                }
            }
        }

        public string StatusTextShort
        {
            get
            {
                if (ApprovedBy.HasValue)
                {
                    return "ผ่านการอนุมัติแล้ว";
                }
                else if (CanceledBy.HasValue)
                {
                    return "ยกเลิกคำขอ";
                }
                else if (RejectedBy.HasValue)
                {
                    return "ไม่ผ่านการอนุมัติ";
                }
                else if (CompletedBy.HasValue)
                {
                    return "สิ้นสุดกระบวนการแล้ว";
                }
                else if (CurrentTaskHistory != null)
                {
                    return "พิจารณาอยู่";
                }
                else
                {
                    return "";
                }
            }
        }
        
    }
}